/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package gmovil_jpa.controladores;

import gmovil_jpa.controladores.exceptions.NonexistentEntityException;
import gmovil_jpa.entidades.TipoPagoPlan;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import gmovil_jpa.entidades.Plan;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author castlerock
 */
public class TipoPagoPlanJpaController {

    public TipoPagoPlanJpaController() {
        emf = Persistence.createEntityManagerFactory("gmovil_jpaPU");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(TipoPagoPlan tipoPagoPlan) {
        if (tipoPagoPlan.getPlanList() == null) {
            tipoPagoPlan.setPlanList(new ArrayList<Plan>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            List<Plan> attachedPlanList = new ArrayList<Plan>();
            for (Plan planListPlanToAttach : tipoPagoPlan.getPlanList()) {
                planListPlanToAttach = em.getReference(planListPlanToAttach.getClass(), planListPlanToAttach.getIdPlan());
                attachedPlanList.add(planListPlanToAttach);
            }
            tipoPagoPlan.setPlanList(attachedPlanList);
            em.persist(tipoPagoPlan);
            for (Plan planListPlan : tipoPagoPlan.getPlanList()) {
                TipoPagoPlan oldTipoPagoPlanOfPlanListPlan = planListPlan.getTipoPagoPlan();
                planListPlan.setTipoPagoPlan(tipoPagoPlan);
                planListPlan = em.merge(planListPlan);
                if (oldTipoPagoPlanOfPlanListPlan != null) {
                    oldTipoPagoPlanOfPlanListPlan.getPlanList().remove(planListPlan);
                    oldTipoPagoPlanOfPlanListPlan = em.merge(oldTipoPagoPlanOfPlanListPlan);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(TipoPagoPlan tipoPagoPlan) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            TipoPagoPlan persistentTipoPagoPlan = em.find(TipoPagoPlan.class, tipoPagoPlan.getIdTipoPagoPlan());
            List<Plan> planListOld = persistentTipoPagoPlan.getPlanList();
            List<Plan> planListNew = tipoPagoPlan.getPlanList();
            List<Plan> attachedPlanListNew = new ArrayList<Plan>();
            for (Plan planListNewPlanToAttach : planListNew) {
                planListNewPlanToAttach = em.getReference(planListNewPlanToAttach.getClass(), planListNewPlanToAttach.getIdPlan());
                attachedPlanListNew.add(planListNewPlanToAttach);
            }
            planListNew = attachedPlanListNew;
            tipoPagoPlan.setPlanList(planListNew);
            tipoPagoPlan = em.merge(tipoPagoPlan);
            for (Plan planListOldPlan : planListOld) {
                if (!planListNew.contains(planListOldPlan)) {
                    planListOldPlan.setTipoPagoPlan(null);
                    planListOldPlan = em.merge(planListOldPlan);
                }
            }
            for (Plan planListNewPlan : planListNew) {
                if (!planListOld.contains(planListNewPlan)) {
                    TipoPagoPlan oldTipoPagoPlanOfPlanListNewPlan = planListNewPlan.getTipoPagoPlan();
                    planListNewPlan.setTipoPagoPlan(tipoPagoPlan);
                    planListNewPlan = em.merge(planListNewPlan);
                    if (oldTipoPagoPlanOfPlanListNewPlan != null && !oldTipoPagoPlanOfPlanListNewPlan.equals(tipoPagoPlan)) {
                        oldTipoPagoPlanOfPlanListNewPlan.getPlanList().remove(planListNewPlan);
                        oldTipoPagoPlanOfPlanListNewPlan = em.merge(oldTipoPagoPlanOfPlanListNewPlan);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = tipoPagoPlan.getIdTipoPagoPlan();
                if (findTipoPagoPlan(id) == null) {
                    throw new NonexistentEntityException("The tipoPagoPlan with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            TipoPagoPlan tipoPagoPlan;
            try {
                tipoPagoPlan = em.getReference(TipoPagoPlan.class, id);
                tipoPagoPlan.getIdTipoPagoPlan();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The tipoPagoPlan with id " + id + " no longer exists.", enfe);
            }
            List<Plan> planList = tipoPagoPlan.getPlanList();
            for (Plan planListPlan : planList) {
                planListPlan.setTipoPagoPlan(null);
                planListPlan = em.merge(planListPlan);
            }
            em.remove(tipoPagoPlan);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<TipoPagoPlan> findTipoPagoPlanEntities() {
        return findTipoPagoPlanEntities(true, -1, -1);
    }

    public List<TipoPagoPlan> findTipoPagoPlanEntities(int maxResults, int firstResult) {
        return findTipoPagoPlanEntities(false, maxResults, firstResult);
    }

    private List<TipoPagoPlan> findTipoPagoPlanEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(TipoPagoPlan.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public TipoPagoPlan findTipoPagoPlan(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(TipoPagoPlan.class, id);
        } finally {
            em.close();
        }
    }

    public int getTipoPagoPlanCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<TipoPagoPlan> rt = cq.from(TipoPagoPlan.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

}
